<template>
  <div class="register-container">
    <!-- 左侧系统信息区域 -->
    <div class="system-info">
      <div class="logo-container">
        <img src="/logo_02.png" alt="系统Logo" class="system-logo" />
      </div>
      <h1 class="system-title">面向东盟的多语言文本对齐系统</h1>
      <p class="system-description">专业的多语言文本处理平台，为中国-东盟合作提供智能化语言服务</p>
    </div>

    <!-- 右侧注册表单 -->
    <div class="register-form">
      <h2>用户注册</h2>
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" type="email" placeholder="请输入邮箱地址"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button color="var(--primary)" type="primary" @click="handleRegister" :loading="loading">注册</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="login-link">
        <router-link to="/login">已有账号？立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const registerFormRef = ref(null)
const loading = ref(false)

const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

const validatePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const validateEmail = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入邮箱地址'))
  } else {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
    if (!emailRegex.test(value)) {
      callback(new Error('请输入正确的邮箱格式'))
    } else {
      callback()
    }
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度必须在3-20个字符之间', trigger: 'blur' }
  ],
  email: [
    { validator: validateEmail, trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { validator: validatePassword, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        // 调用后端注册API
        const response = await axios.post('http://127.0.0.1:5000/api/auth/register', {
          username: registerForm.username,
          email: registerForm.email,
          password: registerForm.password
        })

        if (response.status === 201) {
          ElMessage.success('注册成功！请登录')
          router.push('/login')
        }
      } catch (error) {
        if (error.response && error.response.data) {
          ElMessage.error(error.response.data.message || '注册失败')
        } else {
          ElMessage.error('网络错误，请稍后重试')
        }
      } finally {
        loading.value = false
      }
    }
  })
}

const resetForm = () => {
  registerFormRef.value.resetFields()
}
</script>

<style scoped>
.register-container {
  display: flex;
  height: 100vh;
  background: var(--primary-light);
}

/* 左侧系统信息区域 */
.system-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding:40px 20px 40px 40px;
  background: var(--primary-light);
  color: var(--primary);
}

.logo-container {
  margin-bottom: -1rem;
}

.system-logo {
  max-width:20rem;
  height: auto;
}

.system-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 20px;
  text-align: center;
  line-height: 1.2;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.system-description {
  font-size: 1.1rem;
  line-height: 1.6;
  text-align: center;
  max-width: 400px;
  opacity: 0.95;
  font-weight: 300;
}

/* 右侧注册表单 */
.register-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  backdrop-filter: blur(10px);
  border-left: 1px solid var(--border);
}

.register-form h2 {
  color: var(--primary);
  margin-bottom: 30px;
  font-size: 1.8rem;
  font-weight: 600;
}

.register-form .el-form {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.5);
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 8px 32px var(--shadow);
}

.el-form-item {
    display: flex;
    --font-size: 14px;
    margin-bottom: 1.5rem;
    margin-top: 1.5rem;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  width: 100%;
  max-width: 400px;
}

.login-link a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.login-link a:hover {
  color: var(--primary-strong);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
  }

  .system-info {
    flex: none;
    min-height: 40vh;
    padding: 30px 20px;
  }

  .system-title {
    font-size: 1.8rem;
  }

  .system-description {
    font-size: 1rem;
  }

  .register-form {
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
</style>